﻿@{
    Layout = "~/Views/Shared/_level2.cshtml";
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
    }
} 
<title>系统管理-员工账号</title>
<link href="/SiteUI/Admin/share.css" rel="stylesheet" />
<link href="/SiteUI/Admin/Account.css" rel="stylesheet" />
<script src="/SiteUI/res_sobiz.js"></script>
<link href="/SiteUI/Admin/pvOrgTree.css" rel="stylesheet" />    
@section contentnavi{
    <div id="appTitle">  
        系统管理 
    </div>
    <ul id="slideNavi">
        <li><a class="Lite" href="/Admin/Company"><i class="icon-qiyerenzheng"></i>企业信息<i class="icon-navigatenext arrow-go"></i></a></li>
        <li><a class="Lite" href="/Admin/Organize"><i class="icon-wangluo1"></i>组织架构<i class="icon-navigatenext arrow-go"></i></a></li>
        <li><a class="Lite" href="/Admin/Accounts"><i class="icon-accountbox"></i>员工账号<i class="icon-navigatenext arrow-go"></i></a></li>
        <li><a class="Lite" href="/Admin/Authority"><i class="icon-security"></i>权限设置<i class="icon-navigatenext arrow-go"></i></a></li>
        <li><a class="Lite" href="/Admin/OpenApi"><i class="icon-swaphoriz"></i>开放接口<i class="icon-navigatenext arrow-go"></i></a></li>
        <li><a class="Lite" href="/Admin/Portal"><i class="icon-034library"></i>企业门户<i class="icon-navigatenext arrow-go"></i></a></li>
        <li><a class="Lite" href="/Admin/CloudApp"><i class="icon-nowwidgets"></i>应用管理<i class="icon-navigatenext arrow-go"></i></a></li>
    </ul>
} 
<div class="col-lg-10 col-lg-offset-1">
    <div class="row">
        <div class="FlateSectionHead" id="tabNewAccount"> 
            <h4><i class="icon-security"></i>待审核账号</h4>
        </div>
    </div>
    <div class="row" id="lstNewAccounts">     
        <div class="loading"> 
            <i></i>正在努力加载数据...
        </div>
    </div>
</div>   

<div class="col-lg-10 col-lg-offset-1">   
    <div class="row">
        <div class="Tab-Head " id="tabActiveAccount">
            <h4><i class="icon-accountbox"></i>使用中的账号</h4>
        </div>
    </div>
    <div class="row" id="lstActiveAccounts">
        <div class="col-lg-12 AccountListing">
            <div class="row">
                <div class="col-lg-1">人员姓名</div>
                <div class="col-lg-2">手机号码</div>
                <div class="col-lg-2">主责岗位</div>
                <div class="col-lg-3">所属部门</div>
                <div class="col-lg-2">加入时间</div>
                <div class="col-lg-2">操作</div>
            </div>
        </div>
        <div class="col-lg-12" id="EmployeesList"></div>
        <div class="col-lg-12" id="EmployeesPaging" style="margin-top:2rem;"></div>
    </div>

</div>

<!--岗位选择对话框-->
<div id="ChooseJob" class="row">
    <div class="col-lg-5" id="pot-orgTree-wraper">
        <div class="row" id="pot-ortTitle">
            <h4>组织架构</h4>
        </div>
        <div class="row">
            <div class="col-lg-12" id="pot-orgTree">

            </div>
        </div>
    </div>
    <div class="col-lg-7" style="margin-top:2rem" id="lstOrgPositions">
    </div>
</div>

<script id="tplNewAcnt" type="text/html">
    <div class="col-lg-4 acntItemBox">
        <div class="acntItem">
            <img class="an-headimg" />
            <div class="an-info">
                <span class="an-name"></span><br /><i class="icon-phoneiphone"></i><span class="an-phone"></span><br />
                <i class="icon-mail"></i><span class="an-email"></span>
            </div><br />
            <div class="button btn-big col-lg-6 btn-approve">同意</div>
            <div class="button btn-big col-lg-6 btn-opposition" style="float:right;background-color:rgba(218, 92, 92, 0.84)">拒绝</div>
        </div>
    </div>
</script>

<script>
    Accounts = {
        objid: null,
        acntid:null,
        tplNewAcnt: null,
        ChooseJob: null,
        lstPositions:null,
        approvePositive: function (e) {
            var ai = $("#lstNewAccounts>.acntItemBox");
            var application = $(e.currentTarget).parents(".acntItemBox").data();
            if (application) {
                //将申请信息传递给岗位选择对话框
                Accounts.ChooseJob.vl(application)
                                  //.Content($("#ChooseJob").show())
                                  .RemoveBtn()
                                  .BtnEnter("确定", "", function () {
                                            //$("#ChooseJob").hide().appendTo("body");
                                            Accounts.ChooseJob.hide();
                                            //通过审核，并设置此帐号的岗位
                                            var obj = Accounts.ChooseJob.vl();
                                            var position = Accounts.lstPositions.Slct();
                                            if (!position) {
                                                //判断有没有选择岗位
                                                error("错误", "请为用户选择一个岗位", 3);
                                                return;
                                            }
                                            else {
                                                $.getJSON("/Admin/ApproveAcntApplication", { objid: obj.idstr,acntid:obj.acntidstr, pos: position.idstr, result: "positive" }, function (rtn) {
                                                    if (rtn.IsPositive) {
                                                        //审核通过，从待审核申请列表中删除指定的卡片
                                                        var ai = $("#lstNewAccounts>.acntItemBox");
                                                        for (var i = 0; i < ai.length; i++) {
                                                            if (ai.eq(i).data().idstr == rtn.Data.idstr) {
                                                                ai.eq(i).fadeOut().remove();
                                                                break;
                                                            }
                                                        }
                                                        success("成功", "用户已加入贵司");
                                                    }
                                                    else {
                                                        error("失败", ErrFormate(res_sobiz, rtn.ErrorSrc, rtn.ErrorCode, rtn.Error));
                                                    }
                                                });
                                            }
                                   })
                                  .BtnEsc("取消", "", function () {
                                      //$("#ChooseJob").hide().appendTo("body");
                                      Accounts.ChooseJob.hide();
                                   })
                                  .update()
                                  .show();
            }
        },
        approveNegative: function (e) {
            var ai = $("#lstNewAccounts>.acntItemBox");
            var obj = $(e.currentTarget).parents(".acntItemBox").data();
            if (obj) {
                $.getJSON("/Admin/ApproveAcntApplication", { objid: obj.idstr,acntid:-1, pos: -1, result: "negative", feedback: "" }, function (rtn) {
                    if (rtn.IsPositive) {
                        //操作成功，拒绝相应的申请
                        for (var i = 0; i < ai.length; i++) {
                            if (ai.eq(i).data().idstr == rtn.Data.idstr) {
                                ai.eq(i).fadeOut().remove();
                                break;
                            }
                        }
                        info("成功", "已拒绝此用户的申请");
                    }
                    else {
                        error("失败", ErrFormate(res_sobiz, rtn.ErrorSrc, rtn.ErrorCode, rtn.Error));
                    }
                })
            }
        },
        onUserReady: function () {
            //获取待审核的帐号加入申请
            $.getJSON("/Admin/GetUnapproveApplies", {}, function(rtn){
                $("#lstNewAccounts").children(".loading").hide();
                rtn.Data.NewApplies.forEach(function (a) {
                    var ai = Accounts.tplNewAcnt.clone();
                    ai.data(a); //存储帐号申请卡片对应的申请记录
                    ai.find(".an-headimg").attr("src", a.headimg || "/UserFile/DefaultUserLogo.png");
                    ai.find(".an-phone").vl(a.phone);
                    ai.find(".an-email").vl(a.email);
                    ai.find(".an-name").vl(a.realname);
                    ai.appendTo("#lstNewAccounts").addClass("fladeInUp");

                });
            });
            $.getJSON("/Admin/GetOrganizeTree", { company: -1 }, function (rtn) {
                if (rtn.IsPositive) {
                    //更新组织架构树
                    Accounts.lstOrgTree.vl(rtn.Data.orgtree)
                                             .update();

                }
            });
            $.getJSON("/Admin/GetCompanyAcnts", { company: -1 }, function (rtn) {
                if (rtn.IsPositive) {
                    var acnts = rtn.Data["Acnts"];
                    Accounts.PersonnelList.vl(acnts)
                                          .update();
                    $(".updateJob").on("click", function () {
                        var obj = $(this).data();
                        if (obj) {
                            Accounts.ChooseJob.vl(obj)
                                              .Content()
                                              .RemoveBtn()
                                              .BtnEnter("确定", "", function () {
                                                  var a = Accounts.ChooseJob.vl();
                                                  var position = Accounts.lstPositions.Slct();
                                                  $.getJSON("/Admin/SetAcntPosition", { acnt: a.idstr, pos: position.idstr, postype: "Acnt-Mainposition" }, function (rtn) {
                                                      success("设置成功", "用户的主责岗位已调整", 3);
                                                  })
                                                  //$("#ChooseJob").hide().appendTo("body");
                                                  Accounts.ChooseJob.hide();
                                              })
                                              .BtnEsc("取消", "", function () {
                                                  //$("#ChooseJob").hide().appendTo("body");
                                                  Accounts.ChooseJob.hide();
                                              })
                                              .update()
                                              .show();
                        }
                    })
                }
            });

        },
        onGetOrgTreeEnd: function (rtn) {
            
        },
        lstOrgTree: null,
        LoadOrgPositions: function ($cnt, pos) {
            $.getJSON("/Admin/GetOrgPositions", { objid: pos.idstr }, function (rtn) {
                if (rtn.IsPositive) {
                    Accounts.lstPositions.vl(rtn.Data.positions)
                                         .update();
                }
            });
        }
    }
    $(function () {
        var navis = $("#slideNavi>li>a");
        for (var i = 0; i < navis.length; i++) {
            if (navis.eq(i).attr("href") == window.location.pathname) {
                navis.eq(i).addClass("current");
                break;
            }
        }

       
        Accounts.tplNewAcnt = $($("#tplNewAcnt")[0].innerHTML);


        //组织架构树列表
        Accounts.lstOrgTree = new LiberUI.Lister("#pot-orgTree")
                                            .option("TreeMode", true)
                                            .option("ContentRender", function ($item, o) {
                                                $("<div class='pot-orgItem'>").vl(o.name)
                                                                              .appendTo($item);
                                            })
                                            .on("itemSlcted", Accounts.LoadOrgPositions);

        //岗位树列表
        Accounts.lstPositions = new LiberUI.Lister("#lstOrgPositions")
                                        .option("TreeMode", true)
                                        .option("NodePrefix", "-")
                                        .option("ContentRender", function ($item, o) {
                                            var html = $("<h4>").vl(o.name);                    
                                            html.appendTo($item);
                                        })
                                        .Empty("请选择一个组织架构来查看");


        //企业员工列表
        Accounts.PersonnelList = new LiberUI.Lister("#EmployeesList")
                                              .option("ContentRender", function ($item, o) {
                                                  var row = $("<div class='row AccountDetails' >");
                                                  $("<div class='col-lg-1'>").vl(o.username).appendTo(row);
                                                  $("<div class='col-lg-2'>").vl(o.mphone).appendTo(row);
                                                  $("<div class='col-lg-2'>").vl(o.mposition).appendTo(row);
                                                  $("<div class='col-lg-3'>").vl(o.deptname).appendTo(row);
                                                  var time = new Date(o.jointime).format("yyyy/MM/dd hh:mm:ss");
                                                  $("<div class='col-lg-2'>").vl(time).appendTo(row);
                                                  $("<span class='updateJob' acntid='" + o.idstr + "'>").vl("调整岗位").appendTo($("<div class='col-lg-2'>")).appendTo(row).data(o);
                                                  row.appendTo($item);
                                              })
                                              .Loading("正在加载...");

        //帐号选择对话框
        Accounts.ChooseJob = new LiberUI.Dialoger()
                                        .Content($("#ChooseJob"))
                                        .Title("选择岗位");

        $("body").on("UserReady", Accounts.onUserReady);
       

        $("#lstNewAccounts").delegate(".btn-approve", "click", Accounts.approvePositive);
        $("#lstNewAccounts").delegate(".btn-opposition", "click", Accounts.approveNegative);

    })
</script>

